package ToYesterday.Day08;

public class balancedStringSplit {


    public static void main(String[] args) {
        String str  = "RLLLLRRRLR";
        int count = balanceStringSplit(str);
        System.out.println(count);
    }


    public static int balanceStringSplit(String s){
        char[] str = s.toCharArray();
        int sum=0;
        int count = 0;
        for (int i = 0;i<s.length();i++){
            if (str[i]=='R')sum++;else sum--;
            if (sum==0)count++;
        }
        return count;
    }

    public static int balancedStringSplit(String s) {
        char [] str = s.toCharArray();
        char [] str2 = new char[str.length];
        int i =0, j =0;
        int count=0;
        while (i<str.length){
            if (j==0){
                str2[j]=str[i];
                i++;
                j++;
                continue;
            }
            if (str2[j-1]==str[i]){
                str2[j]=str[i];
                i++;
                j++;
            }else {
                j--;
                i++;
            }
            if (j==0){
                count++;
            }
        }
        return count;
    }

}
